package com.feeyo.prophet.service;

import com.feeyo.prophet.mapper.InterfaceTypeMapper;
import com.feeyo.prophet.mapper.UserInfoMapper;
import com.feeyo.prophet.plugins.Md5Util;
import com.feeyo.prophet.pojo.po.InterfaceType;
import com.feeyo.prophet.pojo.po.UserInfo;
import com.google.common.collect.Lists;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthService {

    private Map<String, UserInfo> users;
    private final UserInfoMapper userInfoMapper;
    private final InterfaceTypeMapper interfaceTypeMapper;

    @Value(value = "${prophet.auth.productName}")
    private String productName;

    @Autowired
    public AuthService(UserInfoMapper userInfoMapper, InterfaceTypeMapper interfaceTypeMapper) {
        this.userInfoMapper = userInfoMapper;
        this.interfaceTypeMapper = interfaceTypeMapper;
    }

    @PostConstruct
    public void init() {
        resetUsers();
    }

    /**
     * 生成 token
     */
    public String generateToken(String appSecret, String timestamp) {
        return Md5Util.md5_32(Md5Util.md5_32(appSecret) + timestamp);
    }

    /**
     * 每小时的第5分钟开始，每5分钟更新一次用户信息
     */
//    @Scheduled(cron = "0 5/5 * * * ?")
    public void resetUsers() {
        List<InterfaceType> interfaceTypes = interfaceTypeMapper.selectAllByProjectName(productName);
        Map<String, InterfaceType> interfaceTypesMap = new HashMap<>();
        for (InterfaceType interfaceType : interfaceTypes) {
            interfaceTypesMap.put(interfaceType.getIid().toString(), interfaceType);
        }

        List<UserInfo> userInfos = userInfoMapper.selectAllByIsDisabled();
        Map<String, UserInfo> users = new HashMap<>();
        for (UserInfo userInfo : userInfos) {
            String interfaceId = userInfo.getInterfaceId();
            String[] split = StringUtils.split(interfaceId, ",");

            if (split == null || split.length == 0) {
                continue;
            }
            List<String> uris = Lists.newArrayList();
            for (String iid : split) {
                InterfaceType interfaceType = interfaceTypesMap.get(iid);
                if (null == interfaceType) {
                    continue;
                }
                uris.add(interfaceType.getNameUrl());
            }
            userInfo.setUris(uris);
            users.put(userInfo.getAppId(), userInfo);
        }
        this.users = users;
    }

    public UserInfo getUserInfoBy(String appId) {
        return users.get(appId);
    }

    public boolean uriMatch(List<String> uris, String uri) {
        if (uris == null) {
            return false;
        }
        if (uris.isEmpty()) {
            return false;
        }
        for (String u : uris) {
            if (u.equals(uri)) {
                return true;
            }
        }
        return false;
    }

}